package com.nakamachizu.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.slim3.controller.upload.FileItem;
import org.slim3.util.StringUtil;

import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;
import com.google.appengine.api.mail.MailService.Attachment;
import com.google.appengine.api.mail.MailService.Message;

/*

添付できるファイルの種類

MIME Type   Filename Extensions
application/pdf pdf
application/rss+xml rss
audio/basic au snd
audio/flac  flac
audio/mid   mid rmi
audio/mp4   m4a
audio/mpeg  mp3
audio/ogg   oga ogg
audio/x-aiff    aif aifc aiff
audio/x-wav wav
image/gif   gif
image/jpeg  jpeg jpg jpe
image/png   png
image/tiff  tiff tif
image/vnd.wap.wbmp  wbmp
image/x-ms-bmp  bmp
text/calendar   ics
text/comma-separated-values csv
text/css    css
text/html   htm html
text/plain  text txt asc diff pot
text/x-vcard    vcf
video/mp4   mp4
video/mpeg  mpeg mpg mpe
video/ogg   ogv
video/quicktime qt mov
video/x-msvideo avi

 */

public class MailUtil {
    
    
    public Map<String,String> attachableExt;
    
    public MailUtil(){
        attachableExt = new HashMap<String,String>();
        attachableExt.put("bmp", "image/x-ms-bmp");
        attachableExt.put("css", "text/css");
        attachableExt.put("csv", "text/comma-separated-values");
        attachableExt.put("gif", "image/gif");
        attachableExt.put("htm", "text/html");
        attachableExt.put("html", "text/html");
        attachableExt.put("jpeg", "image/jpeg");
        attachableExt.put("jpg", "image/jpeg");
        attachableExt.put("jpe", "image/jpeg");
        attachableExt.put("pdf", "application/pdf");
        attachableExt.put("png", "image/png");
        attachableExt.put("rss", "application/rss+xml");
        attachableExt.put("text", "text/plain");
        attachableExt.put("txt", "text/plain");
        attachableExt.put("asc", "text/plain");
        attachableExt.put("diff", "text/plain");
        attachableExt.put("pot", "text/plain");
        attachableExt.put("tiff", "image/tiff");
        attachableExt.put("tif", "image/tiff");
        attachableExt.put("wbmp", "image/vnd.wap.wbmp");
        attachableExt.put("ics", "text/calendar");
        attachableExt.put("vcf", "text/x-vcard");
        attachableExt.put("au", "audio/basic");
        attachableExt.put("snd", "audio/basic");
        attachableExt.put("flac", "audio/flac");
        attachableExt.put("mid", "audio/mid");
        attachableExt.put("rmi", "audio/mid");
        attachableExt.put("m4a", "audio/mp4");
        attachableExt.put("mp3", "audio/mpeg");
        attachableExt.put("oga", "audio/ogg");
        attachableExt.put("ogg", "audio/ogg");
        attachableExt.put("aif", "audio/x-aiff");
        attachableExt.put("aiff", "audio/x-aiff");
        attachableExt.put("aifc", "audio/x-aiff");
        attachableExt.put("wav", "audio/x-wav");
        attachableExt.put("mp4", "video/mp4");
        attachableExt.put("mpeg", "video/mpeg");
        attachableExt.put("mpg", "video/mpeg");
        attachableExt.put("mpe", "video/mpeg");
        attachableExt.put("ogv", "video/ogg");
        attachableExt.put("qt", "video/quicktime");
        attachableExt.put("mov", "video/quicktime");
        attachableExt.put("avi", "video/x-msvideo");

    }

    
    public String getContentType(String fileName){
        String contentType = null;
        if(!StringUtil.isEmpty(fileName)){
            Pattern pattern = Pattern.compile("^.*\\.([^\\.]+)$");
            Matcher matcher = pattern.matcher(fileName);
            String ext = null;
            while(matcher.find()){
                ext = matcher.group(1);
            }
            
            if(!StringUtil.isEmpty(ext)){
                contentType = attachableExt.get(ext);
            }
        }
        
        return contentType;
    }
    
    
    /**
     * Low level API
     * @param toAddress
     * @param fromAddress
     * @param subject
     * @param body
     * @param charSet
     * @param files
     */
    public void send(List<String> toAddress, String fromAddress, String subject, String body, List<FileItem> files) throws IOException{
        MailService ms = MailServiceFactory.getMailService();
        Message message = new Message();
        message.setSubject(subject);
        message.setTo(toAddress);
        message.setSender(fromAddress);
        message.setTextBody(body);
        if(files != null && files.size() > 0){
            List<Attachment> list = new ArrayList<Attachment>();
            for(FileItem item : files){
                Attachment at = new Attachment(item.getFileName(), item.getData());
                list.add(at);
            }
            message.setAttachments(list);
        }
        ms.send(message);
    }
    
    /**
     * Low level API
     * @param toAddress
     * @param fromAddress
     * @param subject
     * @param body
     * @param charSet
     * @param files
     */
    public void sendBcc(List<String> toAddress, String fromAddress, List<String> bcc, String subject, String body, List<FileItem> files) throws IOException{
        MailService ms = MailServiceFactory.getMailService();
        Message message = new Message();
        message.setSubject(subject);
        message.setTo(toAddress);
        message.setBcc(bcc);
        message.setSender(fromAddress);
        message.setTextBody(body);
        if(files != null && files.size() > 0){
            List<Attachment> list = new ArrayList<Attachment>();
            for(FileItem item : files){
                Attachment at = new Attachment(item.getFileName(), item.getData());
                list.add(at);
            }
            message.setAttachments(list);
        }
        ms.send(message);
    }
    
    public static boolean isValidMailAddress(String mailAddress){
        boolean result = true;
        if(StringUtil.isEmpty(mailAddress)){
           result = false; 
        }else{
            try{
                if(!mailAddress.contains("@")){
                    throw(new AddressException());
                }
                InternetAddress address = new InternetAddress(mailAddress);
            }catch(AddressException e){
                result = false;
            }
        }
        return result;
    }
}